/**
 * 
 */
package com.meiwenhui.sishuwujing;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.meiwenhui.sishuwujing.entities.Favorite;

public class MyFavorite extends Fragment {

	private static final String TAG = "MyFavorite:Content";

	private ListView mFavorites;
	private List<Favorite> favorites = new ArrayList<Favorite>(0);

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.i(TAG, TAG);
		initHistory();
		View view = inflater.inflate(R.layout.activity_history, container, false);
		mFavorites = (ListView) view.findViewById(R.id.his_list);
		registerForContextMenu(mFavorites);  
		mFavorites.setAdapter(new FavoriteAdapter(favorites, inflater));
		return view;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_LONG).show();
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		MenuInflater mInflater = new MenuInflater(getActivity());
        mInflater.inflate(R.menu.activity_favorite, menu);  
        super.onCreateContextMenu(menu, v, menuInfo);
	}

	private void initHistory() {
		for (int i = 0; i < 20; i++) {
			Favorite favorite = new Favorite();
			favorites.add(favorite);
		}
	}


	class FavoriteAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private List<Favorite> favorites;

		public FavoriteAdapter(List<Favorite> favorites, LayoutInflater inflater) {
			this.favorites = favorites;
			this.inflater = inflater;
		}

		@Override
		public int getCount() {
			return favorites.size();
		}

		@Override
		public Object getItem(int i) {
			return favorites.get(i);
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(int i, View view, ViewGroup viewgroup) {
			if (view == null) {
				view = inflater.inflate(R.layout.activity_history_listview_item, null);
			}
			TextView mTitle = (TextView) view.findViewById(R.id.his_title);
			TextView mChapter = (TextView) view.findViewById(R.id.his_chapter);
			mTitle.setText(UUID.randomUUID().toString());
			mChapter.setText(new Date().toLocaleString());
			return view;
		}

	}

}
